<?php

include '../inc/db_util.php';

$action = $_GET['act'];

switch ($action) {
    case 'get':
        read();
        break;
    case 'add':
        approve();
        break;
    case 'del':
        reject();
        break;
    default:
        echo 'Wrong command!';
        break;
}

function read() {
    $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
    $sort = isset($_POST['sort']) ? strval($_POST['sort']) : 'nim';
    $order = isset($_POST['order']) ? strval($_POST['order']) : 'asc';
    $cari = isset($_POST['cari_temp']) ? mysql_real_escape_string($_POST['cari_temp']) : '';

    $offset = ($page - 1) * $rows;

    $where = " WHERE nim LIKE '%$cari%' OR nama LIKE '%$cari%' OR alamat LIKE '%$cari%' "
            . " OR telepon LIKE '%$cari%' OR email LIKE '%$cari%' OR tahun_angkatan = '%$cari%' ";

    $quey = "SELECT * FROM tb_temp_mahasiswa m
        LEFT JOIN tb_angkatan a ON a.id_angkatan = m.id_angkatan
	$where ";

    $paging = " ORDER BY $sort $order LIMIT $rows OFFSET $offset";

    $result = array();
    $count = mysql_query($quey) or die(mysql_error());
    $result['total'] = mysql_num_rows($count);
    $row = array();

    $criteria = mysql_query($quey . $paging) or die(mysql_error());

    while ($data = mysql_fetch_array($criteria)) {
        $row[] = array(
            'id' => $data['id_temp_mahasiswa'],
            'nim' => $data['nim'],
            'nama' => $data['nama'],
            'alamat' => $data['alamat'],
            'telepon' => $data['telepon'],
            'email' => $data['email'],
            'angkatan' => $data['id_angkatan'],
            'tahun' => $data['tahun_angkatan']
        );
    }
    $result = array_merge($result, array('rows' => $row));
    echo json_encode($result);
}

function approve() {
    $ids = $_POST['id'];

    $array = json_decode($ids);
    $id = implode("','", $array);
    $count = count($array);

    $query = "INSERT INTO tb_mahasiswa(id_mahasiswa, nim, nama, username, `password`, alamat, telepon, email)
            SELECT t.id_mahasiswa, t.nim, t.nama, t.username, t.`password`, t.alamat, t.telepon, t.email 
            FROM tb_temp_mahasiswa t WHERE id_temp_mahasiswa IN ('" . $id . "')";

    $result = mysql_query($query) or die(mysql_error());
    if ($result) {
        $success = mysql_query("DELETE FROM tb_temp_mahasiswa WHERE id_temp_mahasiswa IN ('" . $id . "')") or die(mysql_error());

        if ($success) {
            echo $count . " Pendaftaran telah di terima";
        }
    } else {
        echo "Gagal menerima $count pendaftaran!";
    }
}

function reject() {
    $ids = $_POST['id'];

    $array = json_decode($ids);
    $id = implode("','", $array);

    $query = "DELETE FROM tb_temp_mahasiswa WHERE id_temp_mahasiswa IN ('" . $id . "')";
    $result = mysql_query($query) or die(mysql_error());

    $count = count($array) > 1 ? "(" . count($array) . ") " : "";

    if ($result) {
        echo $count . "Pendaftaran yang di pilih telah ditolak";
    } else {
        echo "Gagal menghapus $count pendaftaran!";
    }
}

?>